<?php
namespace Api\Controller;
use Api\Model\UsersModel;
use Think\Controller;
class LoginController extends Controller {

    //登录
    public function login() {
        $input = I('post.');
        if(!isset($input['username']) || !isset($input['password']) || $input['username'] == null || $input['password'] == null){
            $data = [
                'status' => 403,
                'info' => '参数错误'
            ];
            $this->ajaxReturn($data);
        }
        if(!$this->verify($input['username'], $input['password'])) {
            $data = [
                'status' => 403,
                'info' => '身份认证错误还想约炮?!'
            ];
            $this->ajaxReturn($data);
        }
        else{
            $data = [
                    'status' => 200,
                    'info' => '登录成功, 可以开始约炮→_→',
                    'nickname' => session('nickname'),
                    'head' => session('head'),
                    'token' => session('token'),
                    'uid' => session('uid')
            ];
            $this->ajaxReturn($data);
        }

    }

    public function register() {
        $input = I('post.');
        if(!isset($input['username']) || !isset($input['password']) || $input['username'] == null || $input['password'] == null){
            $data = [
                'status' => 403,
                'info' => '参数错误'
            ];
            $this->ajaxReturn($data);
        }
        if($this->regist($input['username'], $input['password'])) {
            $data = [
                'status' => 200,
                'info' => '注册成功'
            ];
            $this->ajaxReturn($data);
        }
        $data = [
            'status' => 403,
            'info' => '用户名已使用'
        ];
        $this->ajaxReturn($data);
    }

    private function regist($stunum, $password) {
        $data = [
            'stu_num' => $stunum,
            'password' => sha1($password),
            'head' => 'http://'.$_SERVER['HTTP_HOST'].'/Public/default.jpg',
            'created_at' => time()
        ];
        $users = M('users');
        $num = $users->where(['stu_num' => $stunum])->count();
        if ($num != 0) {
            return false;
        }
        $users->add($data);
        return true;
    }
    //验证
    private function verify($username, $password) {
        $data = [
            'stu_num' => $username,
            'password' => sha1($password)
        ];
        if($this->curl($data)){
            $user = new UsersModel();
            $map = [
                'stu_num' => $username
            ];
            $token = md5(time().$username);
            session('token', $token);
            if($user->where($map)->data(['updated_at' => time(),'token'=>$token])->save()) {
                $info = $user->where($map)->find();
                session('uid', $info['id']);
                session('head', $info['head']);
                session('nickname', $info['nickname']);
                return true;
            }
            else {
                $default_head = 'http://'.__APP__.'/Public/default.jpg';
                $new = [
                    'stu_num' => $username,
                    'head' => $default_head,
                    'signature' => '',
                    'academy' => '',
                    'qq' => '',
                    'weixin' => '',
                    'telephone' => '',
                    'nickname' => $username,
                    'created_at' => time(),
                    'updated_at' => time(),
                    'token' => $token
                ];
                $user->add($new);
                $info = $user->where(['stu_num'=>$username])->find();
                session('head', $default_head);
                session('nickname', $info['nickname']);
                session('uid', $info['id']);
                return true;
            }
        }
        return false;
    }

    //curl
    private function curl($data = []){
        $count = M('users')->where($data)->count();
        if ($count == 1) {
            return true;
        }
        return false;
    }
}